package com.zxmzy.scaffold.core.utils

import android.annotation.SuppressLint
import android.content.Context
import com.zxmzy.scaffold.core.base.BaseApplication
import com.zxmzy.scaffold.core.ext.saveAs
import com.zxmzy.scaffold.core.ext.saveAsUnChecked

/**
 * @author zhangxiaoming
 * @date 2023/7/27
 * @description
 */
//val appContext:Context = BaseApplication.instance.applicationContext
val appContext: Context get() = internalCtx ?: initAndGetAppCtxWithReflection()

@SuppressLint("StaticFieldLeak")
private var internalCtx: Context? = null

@SuppressLint("PrivateApi", "DiscouragedPrivateApi")
private fun initAndGetAppCtxWithReflection(): Context {
    val activityThread = Class.forName("android.app.ActivityThread")
    val ctx = activityThread.getDeclaredMethod("currentApplication").invoke(null) as Context
    internalCtx = ctx
    return ctx
}